<?php

namespace App\Http\Controllers\api\exchange;

use App\Http\Controllers\Controller;
use App\Models\ExChanging;
use App\Models\Integral;
use App\Server\ApiService;
use App\Server\RedisService;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;

class ExChangeController extends Controller
{
    /**
     * Name: 获取兑换商品列表
     * User: 王梓墨
     * Date: 2023/7/5
     * Time:15:50
     * @return \Illuminate\Http\JsonResponse
     */
    public function exChangeIndex()
    {
        $data = Integral::all()->toArray();
        return ApiService::success('获取兑换商品列表成功',$data);
    }

    /**
     * Name: 兑换商品
     * User: 王梓墨
     * Date: 2023/7/5
     * Time:15:51
     * @param Request $request
     */
    public function exChanging(Request $request)
    {
        $data = $request->all();
        //获取用户id
        $userId = 1;
        //获取兑换积分商品id
        $integralId = $data['integralId'];
        //兑换商品需要的积分
        $integral = $data['integral'];
        $integralInfo = Integral::where('id',$integralId)->get()->toArray();
        try {
            //将用户积分减去兑换商品的积分
            $userScore = DB::table('admin')->where('id',$userId)->value('score');
            if ($userScore < $integral)
            {
                return ApiService::fail('积分不足，请坚持每天签到获取积分');
            } else {
                DB::table('admin')->where('id',$userId)
                    ->update([
                        'score' => $userScore - $integral
                    ]);
            }
            //将此商品数量 - 1
            Integral::where('id',$integralId)
                ->update([
                    'num' => Integral::where('id',$integralId)->value('num') - 1
                ]);
            //判断用户是否已经兑换过
            $exsits = ExChanging::where('user_id',$userId)
                ->where('goods_name',$integralInfo[0]['name'])
                ->exists();
            if (!$exsits)
            {
                ExChanging::create([
                    'goods_name' => $integralInfo[0]['name'],
                    'user_id' => $userId,
                    'num' => 1
                ]);
                $json = json_encode($integralInfo);
//                $json = json_decode($json);
                RedisService::getInstance()->set($userId,$json);
            } else {
                ExChanging::where('user_id',$userId)
                    ->where('goods_name',$integralInfo[0]['name'])
                    ->first()
                    ->update([
                        'num' => ExChanging::where('user_id',$userId)
                            ->where('goods_name',$integralInfo[0]['name'])
                            ->value('num') + 1
                    ]);
                $json = json_encode($integralInfo);
//                $json = json_decode($json);
                RedisService::getInstance()->set($userId,$json);
            }
            return ApiService::success('兑换商品成功',$integralInfo);
        } catch (\Exception $e) {
            Log::info($e->getMessage());
        }
    }
}
